package nc.ui.scm.plugin;

import java.util.ArrayList;
import java.util.Arrays;
import nc.ui.pub.ButtonObject;
import nc.vo.jcom.lang.StringUtil;
import nc.vo.scm.plugin.ScmPluginXML;
import nc.vo.scm.plugin.UIPluginMenuInfo;
import nc.vo.scm.plugin.UIPluginsInfo;
import nc.vo.scm.pub.SCMEnv;
import nc.vo.trade.button.ButtonVO;

public class UIPluginFactory
{
  public static UIPluginFactory uipluginfactory = new UIPluginFactory();
  public static final int const_extmenus_strart = 10000;
  public static int const_extmenus_count = 1;

  public static UIPluginFactory getIntance()
  {
    return uipluginfactory;
  }

  public ArrayList<IScmUIPlugin> getExtUIPlugins(String prodcode, String cbilltype, SCMUIContext context) {
    try {
      UIPluginsInfo[] infos = ScmPluginXML.getInstance().getExtUIPlugins(prodcode, cbilltype, null);
      if ((infos == null) || (infos.length <= 0))
        return null;
      ArrayList retlist = new ArrayList();
      for (UIPluginsInfo info : infos) {
        try {
          IScmUIPlugin plug = (IScmUIPlugin)Class.forName(info.getPluginclassname()).newInstance();
          plug.init(context);
          retlist.add(plug);
        } catch (Throwable e) {
          SCMEnv.error(e);
        }
      }
      if (retlist.size() <= 0)
        return null;
      return retlist;
    }
    catch (Throwable e) {
      SCMEnv.error(e);
    }
    return null;
  }

  public ArrayList<IScmUIPlugin> getExtUIPlugins(String prodcode, String cbilltype, String uiclassname, SCMUIContext context) {
    try {
      UIPluginsInfo[] infos = ScmPluginXML.getInstance().getExtUIPlugins(prodcode, cbilltype, uiclassname);
      if ((infos == null) || (infos.length <= 0))
        return null;
      ArrayList retlist = new ArrayList();
      for (UIPluginsInfo info : infos) {
        try {
          IScmUIPlugin plug = (IScmUIPlugin)Class.forName(info.getPluginclassname()).newInstance();
          plug.init(context);
          retlist.add(plug);
        } catch (Throwable e) {
          SCMEnv.error(e);
        }
      }
      if (retlist.size() <= 0)
        return null;
      return retlist;
    }
    catch (Throwable e) {
      SCMEnv.error(e);
    }
    return null;
  }

  public ButtonVO getButtonVO(UIPluginMenuInfo info)
  {
    ButtonVO btnVo = new ButtonVO();
    btnVo.setBtnNo(10000 + const_extmenus_count);
    btnVo.setBtnChinaName(info.getName());
    btnVo.setBtnName(info.getName());
    btnVo.setHintStr(info.getName());
    btnVo.setOperateStatus(null);
    btnVo.setBusinessStatus(null);
    btnVo.setHotKey(info.getHotkey());

    btnVo.setDisplayHotKey(info.getHotkey());
    const_extmenus_count += 1;

    return btnVo;
  }

  public ButtonObject getParentBtn(ButtonObject[] bts, String parentcode)
  {
    if ((StringUtil.isEmpty(parentcode)) || (bts == null) || (bts.length <= 0))
      return null;
    ButtonObject ret = null;
    for (ButtonObject bt : bts) {
      if ((bt.getCode() != null) && (bt.getCode().equals(parentcode)))
        return bt;
      ret = getParentBtn(bt.getChildButtonGroup(), parentcode);
      if (ret != null)
        return ret;
    }
    return null;
  }

  public ButtonObject getButtonObject(ButtonObject[] bts, String parentcode) {
    if ((StringUtil.isEmpty(parentcode)) || (bts == null) || (bts.length <= 0))
      return null;
    ButtonObject ret = null;
    for (ButtonObject bt : bts) {
      if ((bt.getCode() != null) && (bt.getCode().equals(parentcode)))
        return bt;
      ret = getParentBtn(bt.getChildButtonGroup(), parentcode);
      if (ret != null)
        return ret;
    }
    return null;
  }

  public ButtonObject findButtonObject(ButtonObject[] bts, String code) {
    if ((StringUtil.isEmpty(code)) || (bts == null) || (bts.length <= 0))
      return null;
    ButtonObject ret = null;
    for (ButtonObject bt : bts) {
      if ((bt.getCode() != null) && (bt.getCode().equals(code)))
        return bt;
      ret = findButtonObject(bt.getChildButtonGroup(), code);
      if (ret != null)
        return ret;
    }
    return null;
  }

  public ButtonObject[] initUIFoctoryMenus(String prodcode, String cbilltype, ButtonObject[] bts)
  {
    try
    {
      UIPluginsInfo[] infos = ScmPluginXML.getInstance().getExtUIPlugins(prodcode, cbilltype, null);
      if ((infos == null) || (infos.length <= 0))
        return bts;
      ButtonVO btvo = null;
      ButtonObject btn = null; ButtonObject parentbtn = null;

      ArrayList btlist = new ArrayList();
      btlist.addAll(Arrays.asList(bts));

      for (UIPluginsInfo info : infos) {
        UIPluginMenuInfo[] menuinfos = info.getMenuinfo();
        if (menuinfos == null) continue; if (menuinfos.length <= 0)
          continue;
        for (UIPluginMenuInfo menuinfo : menuinfos) {
          btvo = getButtonVO(menuinfo);
          btn = btvo.buildButton();

          if (menuinfo.getParentmenucode() != null) {
            parentbtn = getParentBtn(bts, menuinfo.getParentmenucode());
            if (parentbtn != null)
              parentbtn.addChildButton(btn);
            else
              btlist.add(btn);
          } else {
            btlist.add(btn);
          }
        }
      }
      if (btlist.size() > 0)
        return (ButtonObject[])btlist.toArray(new ButtonObject[btlist.size()]);
      return bts;
    }
    catch (Throwable e) {
      SCMEnv.error(e);
    }
    return null;
  }
}